// components/index-list-item.js
import { COS_HOST } from '../../utils/httpServer';

Component({

  /**
   * 组件的属性列表
   */
  properties: {
    source: Object,
    type: String,
    imgUrl: String,
    hot: Number,
    title: String,
    isHistory: Boolean,
    state: Number,
  },

  observers: {
    type (value) {
      let typeStr = '';
      switch (value) {
        case 'live':
          typeStr = '直播中';
          break;
        case 'video':
          typeStr = '系列视频';
          break;
        case 'act':
          typeStr = '活动';
          break;
        case 'vote':
          typeStr = '投票';
          break;
        case 'news':
          typeStr = '资讯';
          break;
      }
      this.setData({
        typeStr,
      });
    },
    hot (value) {
      let hotStr = value + '';
      if (value >= 100000) {
        hotStr = '10w+';
      }
      this.setData({
        hotStr,
      });
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    typeStr: '',
    hotStr: '',
    realHeight: 0,
    COS_HOST,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    handleTap() {
      let url = '';
      switch (this.properties.type) {
        case 'live':
          url = `/pages/live/live?id=${this.properties.source.video_id}`;
          break;
        case 'video':
          url = `/pages/video/video?id=${this.properties.source.video_id}`;
          break;
        case 'act':
          url = `/pages/activity-detail/activity-detail?id=${this.properties.source.act_id}`;
          break;
        case 'vote':
          if (this.properties.state <= 0) {
            url = `/pages/vote-result/vote-result?voteId=${this.properties.source.vote_id}`;
          } else {
            url = `/pages/vote-detail/vote-detail?voteId=${this.properties.source.vote_id}`;
          }
          break;
        case 'news':
          url =  `/pages/news-detail/news-detail?id=${this.properties.source.news_id}`
          break;
      }
      wx.navigateTo({url});
    },

    onImageLoad(e) {
      const { width, height } = e.detail;
      const wRatio = width / height;
      const hRadio = height / width;
      const realHeight = Math.ceil(Math.max(wRatio, hRadio) * 170);
      this.setData({
        realHeight,
      });
    }
  }
})